package top.xinsix.est.service.impl;
import org.springframework.beans.BeanUtils;
import top.xinsix.est.dto.*;
import top.xinsix.est.mapper.*;
import top.xinsix.est.pojo.*;
import top.xinsix.est.req.AppointmentReq;
import top.xinsix.est.service.MaintainOrderService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;

import javax.annotation.Resource;

@Service
public class MaintainOrderServiceImpl implements MaintainOrderService {

    @Resource
    private MaintainOrderMapper maintainOrderMapper;
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private EvaluateMapper evaluateMapper;
    @Resource
    private MaintainOrderFlowMapper flowMapper;
    @Resource
    private EvaluateDetailMapper detailMapper;

    public MaintainOrder getMaintainOrderById(Long id){
        return maintainOrderMapper.getMaintainOrderById(id);
    }

    public List<MaintainOrder>	getMaintainOrderListByMap(Map<String,Object> param){
        return maintainOrderMapper.getMaintainOrderListByMap(param);
    }

    public Integer getMaintainOrderCountByMap(Map<String,Object> param){
        return maintainOrderMapper.getMaintainOrderCountByMap(param);
    }

    public Integer qdtxAddMaintainOrder(MaintainOrder maintainOrder){
            maintainOrder.setCreatdTime(new Date());
            return maintainOrderMapper.insertMaintainOrder(maintainOrder);
    }

    public Integer qdtxModifyMaintainOrder(MaintainOrder maintainOrder){
        maintainOrder.setUpdatedTime(new Date());
        return maintainOrderMapper.updateMaintainOrder(maintainOrder);
    }

    public Integer qdtxDeleteMaintainOrderById(Long id){
        return maintainOrderMapper.deleteMaintainOrderById(id);
    }

    public Integer qdtxBatchDeleteMaintainOrder(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return maintainOrderMapper.batchDeleteMaintainOrder(param);
    }

    public Page<MaintainOrder> queryMaintainOrderPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = maintainOrderMapper.getMaintainOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<MaintainOrder> maintainOrderList = maintainOrderMapper.getMaintainOrderListByMap(param);
        page.setList(maintainOrderList);
        return page;
     }

    public MaintainOrderDto getDetailByOrderNo(String orderNo) {
        //根据订单编号查询维修订单信息
        Map<String,Object> orderParams = new HashMap<String, Object>();
        orderParams.put("orderNo",orderNo);
        List<MaintainOrder> maintainOrderList = maintainOrderMapper.getMaintainOrderListByMap(orderParams);
        MaintainOrder maintainOrder = null;
        if (maintainOrder != null&& maintainOrderList.size()>0){
            maintainOrder = maintainOrderList.get(0);
        }
        //组装数据
        MaintainOrderDto maintainOrderDto = new MaintainOrderDto();
        BeanUtils.copyProperties(maintainOrder,maintainOrderDto);
        //获取商品型号的信息；根据modelId查询商品型号信息
        Model model = modelMapper.getModelById(maintainOrder.getModelId());
        ModelDto modelDto = new ModelDto(model);
        maintainOrderDto.setModel(modelDto);
        //获取评估信息;根据evaluateId查找评估信息
        Evaluate evaluate = evaluateMapper.getEvaluateById(maintainOrder.getEvaluateId());
        EvaluateDto evaluateDto = new EvaluateDto(evaluate);
        //根据evaluateId查找评估详情列表
        Map<String,Object> detailParams  = new HashMap<String, Object>();
        detailParams.put("evaluateId",evaluate.getId());
        List<EvaluateDetail> detailList = detailMapper.getEvaluateDetailListByMap(detailParams);
        List<EvaluateDetailDto> detailDtoList = new ArrayList<EvaluateDetailDto>();
        //组装数据
        for (EvaluateDetail detail:detailList){
            EvaluateDetailDto evaluateDetailDto = new EvaluateDetailDto();
            BeanUtils.copyProperties(detail,evaluateDetailDto);
            detailDtoList.add(evaluateDetailDto);
        }
        maintainOrderDto.setEvaluate(evaluateDto);
        //获取订单流水信息;根据orderNo查询维修订单流水列表
        Map<String,Object> flowParam  = new HashMap<String, Object>();
        detailParams.put("orderNo",maintainOrder.getOrderNo());
        List<MaintainOrderFlow> flowList = flowMapper.getMaintainOrderFlowListByMap(flowParam);
        List<MaintainOrderFlowDto> flowDtoList = new ArrayList<MaintainOrderFlowDto>();
        //组装数据
        for (MaintainOrderFlow maintainOrderFlow : flowList){
            MaintainOrderFlowDto flowDto = new MaintainOrderFlowDto();
            BeanUtils.copyProperties(maintainOrderFlow,flowDto);
            flowDtoList.add(flowDto);
        }
        return maintainOrderDto;
    }

    public String submit(AppointmentReq appointmentReq, Long evaluateId, Long modelId) {
        //1.保存预约信息；预约手机号，预约日期，预约上门地址
        //2.生成商品订单；用户ID，订单金额，订单状态
        //3.生成流水；订单编号，订单状态
        return null;
    }
}
